package org.amphiprion.descentv2assistant.dao.db;

import java.util.ArrayList;
import java.util.List;

import org.amphiprion.descentv2assistant.entity.GameSession;

import android.content.Context;
import android.database.Cursor;

public class GameSessionDao extends AbstractDbDao {
	private static GameSessionDao instance;

	/**
	 * Hidden constructor.
	 * 
	 * @param context
	 *            the application context
	 */
	private GameSessionDao(Context context) {
		super(context);
	}

	/**
	 * Return the singleton.
	 * 
	 * @param context
	 *            the application context
	 * @return the singleton
	 */
	public static GameSessionDao getInstance(Context context) {
		if (instance == null) {
			instance = new GameSessionDao(context);
		}
		return instance;
	}

	public void persist(GameSession entity) {
		if (isInDb(entity.getId())) {
			update(entity);
		} else {
			create(entity);
		}
	}

	private void create(GameSession entity) {
		String sql = "insert into game_session (" + "id, name, nb_heroes, ol_xp" + ") values (?,?,?,?)";

		Object[] params = new Object[4];
		params[0] = entity.getId();
		params[1] = entity.getName();
		params[2] = entity.getNumberOfHeroes();
		params[3] = entity.getOverlordXp();
		execSQL(sql, params);

	}

	private void update(GameSession entity) {
		String sql = "update game_session set name=?, nb_heroes=?, ol_xp=? where id=?";

		Object[] params = new Object[4];
		params[0] = entity.getName();
		params[1] = entity.getNumberOfHeroes();
		params[2] = entity.getOverlordXp();
		params[3] = entity.getId();

		execSQL(sql, params);

	}

	public boolean isInDb(String id) {
		boolean result = false;
		String sql = "SELECT count(*) from game_session where id='" + id + "'";
		Cursor cursor = getDatabase().rawQuery(sql, null);
		cursor.moveToFirst();
		result = cursor.getInt(0) != 0;
		cursor.close();
		return result;
	}

	public List<GameSession> getAllVisible() {
		String sql = "select " + "id, name, nb_heroes, toshow, ol_xp from game_session where toshow=1 order by name";
		Cursor cursor = getDatabase().rawQuery(sql, null);
		ArrayList<GameSession> result = new ArrayList<GameSession>();

		if (cursor.moveToFirst()) {
			do {
				GameSession entity = new GameSession(cursor);
				result.add(entity);
			} while (cursor.moveToNext());
		}
		cursor.close();
		return result;
	}

	public boolean isVisible(int id) {
		boolean result = false;
		String sql = "SELECT count(*) from game_session where id='" + id + "' and toshow=1";
		Cursor cursor = getDatabase().rawQuery(sql, null);
		cursor.moveToFirst();
		result = cursor.getInt(0) != 0;
		cursor.close();
		return result;
	}

	public void setVisibility(boolean visible, String id) {
		String sql = "update game_session set toshow=? where id=?";

		Object[] params = new Object[2];
		params[0] = visible ? 1 : 0;
		params[1] = id;

		execSQL(sql, params);

	}

	public void writeNotes(GameSession entity, String notes) {
		String sql = "update game_session set notes=? where id=?";

		Object[] params = new Object[2];
		params[0] = notes;
		params[1] = entity.getId();

		execSQL(sql, params);

	}

	public String readNotes(GameSession entity) {
		String sql = "select " + "notes from game_session where id='" + entity.getId() + "'";
		Cursor cursor = getDatabase().rawQuery(sql, null);
		cursor.moveToFirst();
		String result = cursor.getString(0);
		cursor.close();
		return result;
	}

}
